<?php
class Controller_User extends Controller_Client_Layout {	
	function __construct(Request $request, Response $response) {
        parent::__construct($request, $response);				
	}

	public function action_index()
	{
		$this->auto_render = FALSE;
		var_dump(Session::instance()->get('user'));
	}



	/**
	 * đăng ký thành viên
	 */
	public function action_create()
	{
		$this->uniform();

		if (Request::current()->method() == 'POST')
		{	
			// tao user, mac dinh cho no lam member luon.
			$user = Api::load('user')->create($_POST);				
			Flashmessage::getInstance()->doctrine($user);
		}
		
		$this->set_contents(View::client('user/page_user_register'));
	}

	/**
	 * Đăng nhập
	 */
	public function action_login()
	{
		// neu dang nhap roi, thi ko can vao day.
		if (Api::load('user')->is_logged())
		{
			Request::current()->redirect('client/index');	
		}

		$this->uniform();

		if (Request::current()->method() == 'POST')
		{					
			$inputs = Arr::extract($_POST, array('email', 'password'));
			$login = Api::load('user')->login($inputs['email'], $inputs['password']);

			if ($login)
			{
				// dang nhap thanh cong, chuyen ve trang chu
				Request::current()->redirect('client/index');				
			}
			else
			{
				Flashmessage::getInstance()->add(array(
					Flashmessage::ERROR => 'Đăng nhập thất bại bạn ơi, coi chừng capslock đang bật',
				));
			}
		}

		$this->set_contents(View::client('user/page_user_login'));
	}

	/**
	 * Thoát
	 */
	public function action_logout()
	{
		// tat auto render cho le.
		$this->auto_render = FALSE;
		if (Api::load('user')->logout())
		{
			Request::current()->redirect('client/index');
		}
	}



	
}